package observer;

import data.EnvironmentalData;
import data.SoilData;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

// 具体被观察对象 - 环境监测器
public class EnvironmentSensor implements Subject {
    private List<Observer> observers = new ArrayList<>();
    private EnvironmentalData data;
    private static final Random random = new Random();
    public void setData(EnvironmentalData data) {
        this.data = data;
        notifyObservers(data);
    }

    @Override
    public void registerObserver(Observer o) {
        observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        observers.remove(o);
    }

    @Override
    public void notifyObservers(EnvironmentalData data) {
        for (Observer o : observers) {
            o.update(data);
        }
    }
    public SoilData collectSoilData(String location) {
        // 生成随机的土壤数据（模拟真实传感器采集）
        double moisture = 20 + random.nextDouble() * 60; // 20-80%
        double ph = 4.5 + random.nextDouble() * 4.0;    // 4.5-8.5
        double nitrogen = 5 + random.nextDouble() * 45; // 5-50 mg/kg
        double phosphorus = 3 + random.nextDouble() * 27; // 3-30 mg/kg
        double potassium = 50 + random.nextDouble() * 150; // 50-200 mg/kg

        return new SoilData(location, moisture, ph, nitrogen, phosphorus, potassium);
    }
}